package com.kettle.service.kettle.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kettle.mapper.KettleMapper;
import com.kettle.model.kettle.Kettlework;
import com.kettle.service.kettle.KettleService;


@Service("kettleService")
public class KettleServiceImpl extends ServiceImpl<KettleMapper,Kettlework> implements KettleService {
	
	@Resource
	KettleMapper kettleMapper;

	@Value("${kettlepath.lnurlpath}")
	private String lnurlpath;

	/**
	 * 校验是否已存在该任务
	 * @param kmap
	 * @return
	 */
	@Override
	public boolean isshowcount(Map<String,Object> kmap){
		int kcount=kettleMapper.showishavekettle(kmap);
		if(kcount>0){//存在
			return false;
		}else{//不存在
			return true;
		}
	}
	
	/**
	 * 上传附件
	 * @return
	 */
	@Override
	public Map<String,Object> savekettlefile(MultipartFile[] multipartfile,String kfile){
		String contextpath=kfile+"/"+lnurlpath;
		if(multipartfile.length>0){
		 try {
            File f = new File(contextpath); 
            if(!f.exists()){
                f.mkdirs();
            }
            //遍历并保存文件
            for(MultipartFile file :multipartfile){
        	  String filename = file.getOriginalFilename();
              String filepath = contextpath+"/"+filename;
              File newFile = new File(filepath);
			  file.transferTo(newFile);
			  List<String> klist=unZipFiles(newFile,filepath);
			  if(newFile.exists()){
				  newFile.delete();
	          }
             }
		    } catch (Exception e) {
			   e.printStackTrace();
		   }     
        }
		return null;
	}
	/**
	 * 解压文件
	 * @param zipFile
	 * @param descDir
	 * @return
	 * @throws IOException
	 */
	 private List<String> unZipFiles(File zipFile,String descDir)throws IOException{    
		 List<String>  klist=new ArrayList<>();
		 File pathFile = new File(descDir);    
		 if(!pathFile.exists()){       
			 pathFile.mkdirs();     
		 }     
		 //解决zip文件中有中文目录或者中文文件     
		 ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));     
		 for(Enumeration entries = zip.entries();entries.hasMoreElements();){      
			 ZipEntry entry = (ZipEntry)entries.nextElement();       
			 String zipEntryName = entry.getName();       
			 InputStream in = zip.getInputStream(entry);       
			 String outPath = (descDir+zipEntryName).replaceAll("\\*", "/");;       
			 //判断路径是否存在,不存在则创建文件路径       
			 File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));       
			 if(!file.exists()){         
			    file.mkdirs();       
			 }      
			 //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压       
			 if(new File(outPath).isDirectory()){         
				 continue;       
			  }       
			 //获取文件名  
			 klist.add(zipEntryName);
			 //输出文件路径信息     
			 System.out.println(outPath);       
			 OutputStream out = new FileOutputStream(outPath);       
			 byte[] buf1 = new byte[1024];       
			 int len;       
			 while((len=in.read(buf1))>0)       { 
			    out.write(buf1,0,len);      
			 }   
			 in.close();       
			 out.close();     
		 }   
		 zip.close();//关闭解压
		 return klist;
	 } 
}
